package gsp420;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

/*
#include <stdio.h>
#include <conio.h>
#include <windows.h>
struct XY{short x, y;};
struct Entity{XY p; char icon;};
struct Game
{
	Entity player;
	XY size;
	HANDLE h;
	COORD origin;
	Game()
	{
		Entity p = {{4,5}, 1};
		player = p;
		h = GetStdHandle(STD_OUTPUT_HANDLE);
		COORD o = {0,0};
		origin = o;
		XY s = {15, 10};
		size = s;
	}
	void draw()
	{
		SetConsoleCursorPosition(h, origin);
		for(int y = 0; y < size.y; ++y){
			for(int x = 0; x < size.x; ++x){
				if(player.p.x == x && player.p.y == y)
					putchar(player.icon);
				else	putchar('.');
			}
			putchar('\n');
		}
	}
	void handleInput(int input)
	{
		switch(input){
		case 'w':	player.p.y--;	break;
		case 'a':	player.p.x--;	break;
		case 's':	player.p.y++;	break;
		case 'd':	player.p.x++;	break;
		}
	}
};
int main(int argc, char ** argv){
	int input;
	Game g;
	do{
		g.draw();
		input = _getch();
		g.handleInput(input);
	}while(input != 'q');
}
 */

public class CGamePort extends JPanel implements Runnable, KeyListener
{
	class XY{short x, y;XY(int x, int y){this.x=(short)x;this.y=(short)y;}}
	class Entity{XY p; int icon;Entity(XY p, int icon){this.p=p;this.icon=icon;}}
	class Game
	{
		Entity player;
		XY size;
		Game()
		{
			player = new Entity(new XY(4,5), 0x0000ff);
			size = new XY(15, 10);
		}
		void draw(Graphics g)
		{
			final int W = 16, H = 16;
			for(int y = 0; y < size.y; ++y){
				for(int x = 0; x < size.x; ++x){
					if(player.p.x == x && player.p.y == y)
					{
						g.setColor(new Color(player.icon));
						g.fillRect(x*W, y*H, W, H);
					}
					else	
					{
						g.setColor(Color.WHITE);
						g.fillRect(x*W, y*H, W, H);
					}
				}
			}
		}
		void handleInput(int input)
		{
			switch(input){
			case KeyEvent.VK_W:	player.p.y--;	break;
			case KeyEvent.VK_A:	player.p.x--;	break;
			case KeyEvent.VK_S:	player.p.y++;	break;
			case KeyEvent.VK_D:	player.p.x++;	break;
			}
		}
	}
	Game g;
	boolean running;
	public static void main(String[] args)
	{
		JFrame jf = new JFrame("c game port");
		CGamePort cgp = new CGamePort();
		jf.add(cgp);
		jf.setSize(400, 400);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setVisible(true);
		cgp.requestFocus();
	}
	
	CGamePort()
	{
		g = new Game();
		addKeyListener(this);
		Thread t = new Thread(this);
		t.start();
	}
	
	public void run()
	{
		running = true;
		do{
			repaint();
			try{Thread.sleep(100);}catch(Exception e){}
		}while(running);
	}
	
	
	public void paintComponent(Graphics gcontext)
	{
		g.draw(gcontext);
	}

	public void keyPressed(KeyEvent e)
	{
		running = e.getKeyCode() != KeyEvent.VK_Q;
		g.handleInput(e.getKeyCode());
	}

	public void keyReleased(KeyEvent arg0){}
	public void keyTyped(KeyEvent arg0){}
}
